package cn.rentaotao.spring.web;

import cn.rentaotao.spring.data.Spittle;
import cn.rentaotao.spring.service.SpittleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;

import java.security.Principal;
import java.util.List;

/**
 * @author rtt
 * @date 2022/5/26 15:23
 */
@Controller
public class SpittleSocketController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SpittleSocketController.class);

    private final SpittleService spittleService;

    @Autowired
    public SpittleSocketController(SpittleService spittleService) {
        this.spittleService = spittleService;
    }

    /**
     * 无返回值路径
     *
     * @param spittle 客户端传输数据
     */
    @MessageMapping("/spittle/trade")
    public void send(Spittle spittle) {

        Assert.notNull(spittle, "The request body is empty");

        LOGGER.info("Receive from client: {}", spittle.toString());
    }

    /**
     * 有返回值路径
     *
     * @param principal 用户许可证
     * @return 服务端数据
     */
    @SubscribeMapping("/spittle/get")
    public List<Spittle> getSpittles(Principal principal) {

        LOGGER.info("订阅路径 /spittle/get, 用户名称: {}", principal.getName());

        return spittleService.getSpittleList(0, 10);
    }
}
